<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="机构编号" prop="schoolCode">
        <el-input v-model="queryParams.schoolCode" placeholder="请输入机构编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="机构名称" prop="schoolName">
        <el-input v-model="queryParams.schoolName" placeholder="请输入机构名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="社会信用代码" prop="creditCode">
        <el-input v-model="queryParams.creditCode" placeholder="请输入社会信用代码" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="办学许可证号" prop="schoolPermitCode">
        <el-input v-model="queryParams.schoolPermitCode" placeholder="请输入办学许可证号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="法定代表人" prop="legalPerson">
        <el-input v-model="queryParams.legalPerson" placeholder="请输入法定代表人" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="联系人" prop="contactName">
        <el-input v-model="queryParams.contactName" placeholder="请输入联系人" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="联系电话" prop="contactTel">
        <el-input v-model="queryParams.contactTel" placeholder="请输入联系电话" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable @keyup.enter.native="handleQuery">
          <el-option v-for="dict in dict.type.basics_training_state" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['basics:training:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="trainingList" border stripe>
      <el-table-column label="" align="center" prop="id" width="60"/>
      <el-table-column label="机构编号" align="center" prop="schoolCode" width="120"/>
      <el-table-column label="机构logo" align="center" prop="logo">
        <template slot-scope="{row}">
          <image-preview :src="row.logo" :width="30" :height="30"/>
        </template>
      </el-table-column>
      <el-table-column label="机构名称" align="center" prop="schoolName" width="220"/>
      <el-table-column label="营业执照照片" align="center" prop="businessLicense" width="100">
        <template slot-scope="{row}">
          <image-preview :src="row.businessLicense" :width="30" :height="30"/>
        </template>
      </el-table-column>
      <el-table-column label="社会信用代码" align="center" prop="creditCode" width="160"/>
      <el-table-column label="办学许可证号" align="center" prop="schoolPermitCode" width="160"/>
      <el-table-column label="注册资本" align="center" prop="registCapital" />
      <el-table-column label="法定代表人" align="center" prop="legalPerson" width="100"/>
      <el-table-column label="所属地区" align="center" prop="areaCode" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="联系人" align="center" prop="contactName" />
      <el-table-column label="联系电话" align="center" prop="contactTel" width="120"/>
      <el-table-column label="机构简介" align="center" prop="remark" />
      <el-table-column label="客服电话" align="center" prop="servicesMobile" width="120"/>
      <el-table-column label="机构官网" align="center" prop="homepage" width="160"/>
      <el-table-column label="机构状态" align="center" prop="state" fixed="right" width="90">
        <template slot-scope="{row}">
          <dict-tag :options="dict.type.basics_training_state" :value="row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="220">
        <template slot-scope="{row}">
          <el-button size="mini" type="text" icon="el-icon-user" @click="handleShowAdmin(row)">管理员</el-button>
          <el-button size="mini" type="text" icon="el-icon-search" @click="handleDetail(row)">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-document-checked" @click="handleDetail(row)" v-if="row.state === 1" v-hasPermi="['basics:training:check']">审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改培训学校基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>

      <el-descriptions class="margin-top" :column="1" size="mini" labelClassName="label_class" contentClassName="content_class" border>
        <el-descriptions-item label="机构编号">
          {{form.schoolCode}}
        </el-descriptions-item>
        <el-descriptions-item label="机构名称">
          {{form.schoolName}}
        </el-descriptions-item>
        <el-descriptions-item label="营业执照照片">
          {{form.businessLicense}}
        </el-descriptions-item>
        <el-descriptions-item label="社会信用代码">
          {{form.creditCode}}
        </el-descriptions-item>
        <el-descriptions-item label="办学许可证号">
          {{form.schoolPermitCode}}
        </el-descriptions-item>
        <el-descriptions-item label="注册资本">
          {{form.registCapital}}
        </el-descriptions-item>
        <el-descriptions-item label="法定代表人">
          {{form.legalPerson}}
        </el-descriptions-item>
        <el-descriptions-item label="所属地区">
          {{form.areaCode}}
        </el-descriptions-item>
        <el-descriptions-item label="详细地址">
          {{form.address}}
        </el-descriptions-item>
        <el-descriptions-item label="联系人">
          {{form.contactName}}
        </el-descriptions-item>
        <el-descriptions-item label="联系电话">
          {{form.contactTel}}
        </el-descriptions-item>
        <el-descriptions-item label="机构简介">
          {{form.remark}}
        </el-descriptions-item>
        <el-descriptions-item label="客服电话">
          {{form.servicesMobile}}
        </el-descriptions-item>
        <el-descriptions-item label="机构官网">
          {{form.homepage}}
        </el-descriptions-item>
        <el-descriptions-item label="机构logo">
          {{form.logo}}
        </el-descriptions-item>
        <el-descriptions-item label="经营范围">
          {{form.businessScope}}
        </el-descriptions-item>
        <el-descriptions-item label="资质文件列表">
          {{form.fileIds}}
        </el-descriptions-item>
      </el-descriptions>

      <div slot="footer" align="center" class="dialog-footer">
        <el-button type="primary" v-if="form.state === 1" @click="handlePass(form.id, 2)">审核通过</el-button>
        <el-button type="danger" v-if="form.state === 1" @click="handleNoPass(form.id, 3)">审核不通过</el-button>
        <el-button type="danger" v-else @click="cancel">关闭</el-button>
      </div>
    </el-dialog>

    <SchoolAdmin ref="schoolAdminRef"/>

  </div>
</template>

<script>
import { listTraining, getTraining, checkTraining } from "@/api/train/training/school";
import SchoolAdmin from "@/views/train/training/components/school_admin";

export default {
  name: "Training",
  components: {SchoolAdmin},
  dicts: ['basics_training_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 培训学校基础信息表格数据
      trainingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        schoolCode: null,
        schoolName: null,
        businessLicense: null,
        creditCode: null,
        schoolPermitCode: null,
        legalPerson: null,
        contactName: null,
        contactTel: null,
        state: null,
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        schoolName: [
          { required: true, message: "机构名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询培训学校基础信息列表 */
    getList() {
      this.loading = true;
      listTraining(this.queryParams).then(response => {
        this.trainingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        schoolCode: null,
        schoolName: null,
        businessLicense: null,
        creditCode: null,
        schoolPermitCode: null,
        registCapital: null,
        legalPerson: null,
        areaCode: null,
        address: null,
        contactName: null,
        contactTel: null,
        remark: null,
        servicesMobile: null,
        homepage: null,
        logo: null,
        businessScope: null,
        fileIds: null,
        state: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleDetail(row) {
      this.reset();
      getTraining(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "培训学校基础信息";
      });
    },
    handleNoPass(id, state) {
      this.$prompt('请输入审核不通过的原因？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputType: 'textarea',
        inputValidator: (value) => {
          if (!value || value.length > 300) {
            return false
          }
        },
        inputErrorMessage: '请输入审核不通过的原因，字数不超过300个！'
      }).then(({value}) => {
        checkTraining({id: id, state: state, remarks: value}).then(() => {
          this.open = false
          this.$message.success('驳回成功')
          this.getList()
        }).catch(response => {
          this.$message.error(response.data.message)
          this.open = false
        })
      })
    },
    handlePass(id, state) {
      this.$confirm('确认审核通过吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        checkTraining({id: id, state: state}).then(() => {
          this.open = false
          this.$message.success('审核通过')
          this.getList()
        }).catch(response => {
          this.$message.error(response.data.message)
          this.open = false
        })
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('training/school/export', {
        ...this.queryParams
      }, `培训学校列表_${new Date().getTime()}.xlsx`)
    },
    handleShowAdmin(row) {
      this.$refs.schoolAdminRef.openAdmin(row.id)
    }
  }
};
</script>
